package com.liaoban.app.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Selection;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.liaoban.app.R;
import com.liaoban.app.helper.ToastHelper;
import com.liaoban.app.ui.base.BaseToolbarActivity;
import com.liaoban.app.util.VerifyUtil;
import com.liaoban.app.widget.ClearEditText;
import com.liaoban.app.widget.MyTextWatcher;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ybc on 2018/7/6 0006.
 * 注册第一步
 */
public class RegisterActivity extends BaseToolbarActivity {

    @BindView(R.id.et_phone)
    ClearEditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.iv_see)
    ImageView ivSee;
    @BindView(R.id.tv_next)
    TextView tvNext;

    @Override
    protected String getNewTitle() {
        return getString(R.string.register);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initListener();
    }

    private void initListener() {
        MyTextWatcher myTextWatcher = new MyTextWatcher(() -> {
            if (VerifyUtil.isMobileNO(etPhone.getText().toString().trim()) &&
                    etPwd.getText().toString().trim().length() >= 6) {
                tvNext.setEnabled(true);
                tvNext.setBackgroundResource(R.drawable.shape_yellow);
            } else {
                tvNext.setEnabled(false);
                tvNext.setBackgroundResource(R.drawable.shape_line);
            }
        });
        etPhone.addTextChangedListener(myTextWatcher);
        etPwd.addTextChangedListener(myTextWatcher);
    }

    @OnClick(R.id.iv_see)
    public void see() {
        if (ivSee.isSelected()) {
            // 显示为密码
            etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            // 使光标始终在最后位置
            Editable editable = etPwd.getText();
            Selection.setSelection(editable, editable.length());
            ivSee.setSelected(false);
        } else {
            // 显示为普通文本
            etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            // 使光标始终在最后位置
            Editable editable = etPwd.getText();
            Selection.setSelection(editable, editable.length());
            ivSee.setSelected(true);
        }
    }

    @OnClick(R.id.tv_next)
    public void next() {
        String mobile = etPhone.getText().toString().trim();
        String pwd = etPwd.getText().toString().trim();
        startActivityForResult(RegisterTwoActivity.newIntent(this, mobile, pwd), 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0 && resultCode == 0) { //注册成功
            finish();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
